package fxLib
{
	import fl.transitions.Tween;
	import fl.transitions.TweenEvent;
	import fl.transitions.easing.*;
	
	import flash.display.MovieClip;
	
	public class FXSlide
	{
		private static const nSLIDE_DISTANCE:Number           = null;
		private static const nSLIDE_TO_XPOS:Number            = null;
		private static const nSLIDE_TO_YPOS:Number            = null;
		private static const nSLIDE_DURATION:Number           = null;
		
		// set defaults for gettersetters
		private var mnSlideDistance:Number                    = nSLIDE_DISTANCE;
		private var mnSlideToXPos:Number                      = nSLIDE_TO_XPOS;
		private var mnSlideToYPos:Number                      = nSLIDE_TO_YPOS;
		private var mnSlideDuration:Number                    = nSLIDE_DURATION;
		
		private var mcSlideTweenX:Tween;
		private var mcSlideTweenY:Tween;
		
		private var mbSlideDone:Boolean;
		
		// constructor
		// call this function to make an object slide up while fading in
		public function FXSlide( acMCtoSlide:MovieClip, acParams:Object = null )
		{
			// accept parameters if given, in Object format
			if (acParams != null)
			{
				if ( !isNaN( acParams.slideToXPos ) )
				{
					mnSlideToXPos       = acParams.slideToXPos;
				}
				else
				{
					mnSlideToXPos       = acMCtoSlide.x;
				}
				
				if ( !isNaN( acParams.slideToYPos ) )
				{
					mnSlideToYPos       = acParams.slideToYPos;
				}
				else
				{
					mnSlideToYPos       = acMCtoSlide.y;
				}
				
				if ( !isNaN( acParams.slideDistance ) )
					mnSlideDistance     = acParams.slideDistance;
				if ( !isNaN( acParams.slideDuration ) )
					mnSlideDuration     = acParams.slideDuration;
			}
			
			var bOKTOSlide:Boolean      = true;
			
			// defensive code
			if ( isNaN(mnSlideDistance) || isNaN(mnSlideDuration) )
			{
				bOKTOSlide              = false; // we cannot go forward
			}
			
			if (bOKTOSlide)
			{
				mbSlideDone             = false;
				mcSlideTweenX            = new Tween( acMCtoSlide, "x", Strong.easeOut, (mnSlideToXPos + mnSlideDistance), mnSlideToYPos, mnSlideDuration, true );
				mcSlideTweenY            = new Tween( acMCtoSlide, "y", Strong.easeOut, (mnSlideToYPos + mnSlideDistance), mnSlideToYPos, mnSlideDuration, true );
				
				mcSlideTweenY.addEventListener( TweenEvent.MOTION_FINISH, slideFinished, false, 0, true );
			}
			else
			{
				destroy();
			}
		}
		
		public function destroy():void
		{
			mcSlideTweenX              = null;
			mcSlideTweenY              = null;
			
			//trace("[FXSlide destroy]");
		}
		
		public function stop():void
		{
			if (!mbSlideDone)
			{
				mcSlideTweenX.stop();
				mcSlideTweenY.stop();
				mcSlideTweenY.removeEventListener( TweenEvent.MOTION_FINISH, slideFinished, false );
			}
			else
			{
				//trace("[FXSlide stop] SlideDone, huh?");
			}
			
			destroy();
		}
		
		private function slideFinished( e:TweenEvent ):void
		{
			mbSlideDone                 = true;
			mcSlideTweenY.removeEventListener( TweenEvent.MOTION_FINISH, slideFinished, false );
			
			destroy();
		}
		
		//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
		
		public function set slideDistance( anSlideDistance:Number ):void
		{
			mnSlideDistance = anSlideDistance;
		}
		
		public function get slideDistance():Number
		{
			return mnSlideDistance;
		}
		
		public function set slideToYPos( anSlideToYPos:Number ):void
		{
			mnSlideToYPos = anSlideToYPos;
		}
		
		public function get slideToYPos():Number
		{
			return mnSlideToYPos;
		}
		
		public function set slideDuration( anSlideDuration:Number ):void
		{
			mnSlideDuration = anSlideDuration;
		}
		
		public function get slideDuration():Number
		{
			return mnSlideDuration;
		}
		
		//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
		
	}
}